using UnityEngine;
using UnityEngine.UI;

namespace CGF
{
    public class CSlider : MonoBehaviour
    {
        //直线方程类，用于计算数据的线性关系
        class Line
        {
            public float k, b;
            public Vector2 p1, p2;
            public float radian;
            public void SetValue(float x1, float y1, float x2, float y2)
            {
                this.p1 = new Vector2(x1, y1);
                this.p2 = new Vector2(x2, y2);
                k = (y2 - y1) / (x2 - x1);
                b = y1 - k * x1;
                radian = Mathf.Atan2(y2 - y1, x2 - x1);
            }
        }

        public Image fill;
        [Space]
        public Image head;
        public float left = -50;
        public float right = 50;
        [Space]
        public bool ctrlHeadAlpha;
        public float leftAlpha0;
        public float leftAlpha1;
        public float rightAlpha1;
        public float rightAlpha0;

        private Line leftAlpha = new Line();
        private Line rightAlpha = new Line();

        public float value
        {
            get
            {
                if (fill)
                {
                    if (fill.type == Image.Type.Filled)
                    {
                        return fill.fillAmount;
                    }
                    else
                    {
                        if (fill.sprite)
                        {
                            return fill.rectTransform.sizeDelta.x / fill.sprite.texture.width;
                        }
                    }
                }
                return 0;
            }
            set
            {
                float v = Mathf.Clamp01(value);
                if (fill)
                {
                    if (fill.type == Image.Type.Filled)
                    {
                        fill.fillAmount = v;
                    }
                    else
                    {
                        if (fill.sprite)
                        {
                            Vector2 size = fill.rectTransform.sizeDelta;
                            size.x = fill.sprite.texture.width * v;
                            fill.rectTransform.sizeDelta = size;
                        }
                    }

                    if (head)
                    {
                        //头部跟随的位置
                        Vector2 pos = head.rectTransform.anchoredPosition;
                        pos.x = left + (right - left) * v;
                        head.rectTransform.anchoredPosition = pos;

                        if (ctrlHeadAlpha)
                        {
                            //头部在哪个位置开始出现
                            if (leftAlpha1 > 0)
                            {
                                if (v < leftAlpha0)
                                {
                                    SetHeadAlpha(0);
                                }
                                else if (v >= leftAlpha0 && v <= leftAlpha1)
                                {
                                    leftAlpha.SetValue(leftAlpha0, 0, leftAlpha1, 1);
                                    SetHeadAlpha(leftAlpha.k * v + leftAlpha.b);
                                }
                                else
                                {
                                    SetHeadAlpha(1);
                                }
                            }

                            //头部在哪个位置开始隐藏
                            if (rightAlpha1 > 0)
                            {
                                if (v >= rightAlpha1 && v <= rightAlpha0)
                                {
                                    rightAlpha.SetValue(rightAlpha1, 1, rightAlpha0, 0);
                                    SetHeadAlpha(rightAlpha.k * v + rightAlpha.b);
                                }
                                else if (v >= rightAlpha0)
                                {
                                    SetHeadAlpha(0);
                                }
                            }
                        }
                    }
                }
            }
        }

        private void SetHeadAlpha(float a)
        {
            Color color = head.color;
            color.a = a;
            head.color = color;
        }
    }
}